package com.portal.cwegportal.service;

import com.appcenter.jz.Logger.BusinessLogger;
import com.appcenter.jz.Logger.LoggerGenre;
import com.appcenter.jz.page.PagedResult;
import com.appcenter.jz.unit.ObjectUnit;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.portal.cwegportal.input.addressbook.CreateAddressBookInput;
import com.portal.cwegportal.input.addressbook.DeleteAddressBookInput;
import com.portal.cwegportal.input.addressbook.EditAddressBookInput;
import com.portal.cwegportal.input.addressbook.GetAddressBookInput;
import com.portal.cwegportal.input.newscollege.PageLoadInput;
import com.portal.cwegportal.mapper.AddressBookMapper;
import com.portal.cwegportal.model.AddressBook;
import com.portal.cwegportal.model.AddressBookDto;
import com.portal.cwegportal.model.NewsCollege;
import com.portal.cwegportal.model.TreeBuilder;
import com.portal.cwegportal.output.addressbook.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;

import static org.apache.http.util.TextUtils.isBlank;
/**
 *  创建人:王增立
 *  备注: 通讯录说明
 *  时间: 2019/4/26 14:43
 *  参数:
 */
@Service
public class AddressBookService {
    @Autowired
    AddressBookMapper addressBookMapper;
/**
 *  创建人:王增立
 *  备注: 创建通讯录
 *  时间: 2019/4/26 14:43
 *  参数:
 */
    @Transactional
    public CreateAddressBookOutput createAddressBook(CreateAddressBookInput input) throws Exception {
        CreateAddressBookOutput output = new CreateAddressBookOutput();
        AddressBookDto AddressBook = new AddressBookDto();
        ObjectUnit.Copy(input.getItem(), AddressBook);

        AddressBook.setGmtCreate(new Date());
        AddressBook.setDeleted(false);
        addressBookMapper.insertSelective(AddressBook);
        AddressBook.setKey(AddressBook.getId().toString());
        AddressBook.setValue(AddressBook.getId().toString());
        AddressBook.setTitle(AddressBook.getName());
        output.setItem(AddressBook);
        return output;
    }
    /**
     *  创建人:王增立
     *  备注: 删除通讯录
     *  时间: 2019/4/26 14:44
     *  参数:
     */
    public DeleteAddressBookOutput deleteAddressBook(DeleteAddressBookInput input) {
        DeleteAddressBookOutput output = new DeleteAddressBookOutput();
        AddressBook oldEntity = addressBookMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setDeleted(true);
        addressBookMapper.updateByPrimaryKey(oldEntity);
        output.setItem(oldEntity);
        return output;
    }

    /**
     *  创建人:王增立
     *  备注: 编辑通讯录
     *  时间: 2019/4/26 14:44
     *  参数:
     */
    @Transactional
    public EditAddressBookOutput editAddressBook(EditAddressBookInput input) throws Exception {
        EditAddressBookOutput output = new EditAddressBookOutput();
        AddressBook oldEntity = addressBookMapper.selectByPrimaryKey(input.getItem().getId());
        Date dt = new Date();
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        oldEntity.setGmtModified(dt);
        oldEntity.setName(input.getItem().getName());
        oldEntity.setPhone(input.getItem().getPhone());
        oldEntity.setWorkPhone(input.getItem().getWorkPhone());
        oldEntity.setDepartment(input.getItem().getDepartment());
        oldEntity.setStation(input.getItem().getStation());
        oldEntity.setEmail(input.getItem().getEmail());
        oldEntity.setRemark(input.getItem().getRemark());

        addressBookMapper.updateByPrimaryKey(oldEntity);
        output.setItem(oldEntity);
        return output;
    }
/**
 *  创建人:王增立
 *  备注: 获取个人通讯录
 *  时间: 2019/4/26 14:44
 *  参数:
 */
    public GetAddressBookOutput getAddressBookPerson(GetAddressBookInput input) throws Exception {
        GetAddressBookOutput output = new GetAddressBookOutput();
        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize(), "Id desc");


       List<AddressBookDto> ls = addressBookMapper.selectPersonList(input);
        PageInfo<AddressBookDto> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<AddressBookDto>(page.getList(), (int) page.getTotal()));
        return output;

    }
/**
 *  创建人:王增立
 *  备注: 页面加载
 *  时间: 2019/4/26 14:44
 *  参数:
 */
    public PageLoadOutput pageLoad(PageLoadInput input) throws Exception {
        PageLoadOutput output = new PageLoadOutput();
        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize(), "Id desc");

        Example example = new Example(NewsCollege.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleted", 0);
        List<AddressBook> ls = addressBookMapper.selectByExample(example);

        PageInfo<AddressBook> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<AddressBook>(page.getList(), (int) page.getTotal()));
        return output;
    }
/**
 *  创建人:王增立
 *  备注: 根据ID获取通讯录信息
 *  时间: 2019/4/26 14:45
 *  参数:
 */
    public GetAddressBookOutput getAddressBookById(GetAddressBookInput input) throws Exception {
        GetAddressBookOutput output = new GetAddressBookOutput();
        //logger.error("log4j2 error!");
        Example example = new Example(NewsCollege.class);
        Example.Criteria criteria = example.createCriteria();

        AddressBook  object = addressBookMapper.selectByPrimaryKey(input.getId());

        //ObjectUnit.Copy(object,output);
        output.setItem(object);
        return output;

    }
/**
 *  创建人:王增立
 *  备注: 调用组织
 *  时间: 2019/4/26 14:45
 *  参数:
 */
    public GetAddressBookOutput getAddressBookOrg(GetAddressBookInput input) throws Exception {

        GetAddressBookOutput output = new GetAddressBookOutput();
        //List<AddressBookDto> orgDtos = GetOrgTreeList(input);
        //output.setAddressBooks(orgDtos);
        List<TreeBuilder.Node> treeNodes=buildTreeList(input);
        output.setTreeNodes(treeNodes);
        BusinessLogger.Info(LoggerGenre.View, "获取通讯录信息", input);
        return output;
    }

/**
 *  创建人:王增立
 *  备注: 组装树型结构
 *  时间: 2019/4/26 14:46
 *  参数:
 */
    public List<TreeBuilder.Node> buildTreeList(GetAddressBookInput input) throws Exception {

        // 获取全部目录节点
        List<TreeBuilder.Node> nodes = addressBookMapper.selectOrgList(input);

        // 拼装树形
        List<TreeBuilder.Node> list = new TreeBuilder().buildTree(nodes);

        return list;
    }
}
